//
//  XWHCalendarDayView.swift
//  XiaoweHealthy
//
//  Created by Riven on 2022/4/26.
//

import UIKit
import JTAppleCalendar

class XWHCalendarDayView: UIView, JTACMonthViewDataSource & JTACMonthViewDelegate {
    
    var dateType: XWHHealthyDateSegmentType {
        .day
    }

    lazy var topBgView = UIView()
    lazy var preNextView = XWHCalendarPreNextBtnView(dateType: dateType)
    lazy var weekIndicatiorView = XWHCalendarWeekIndicatorView(config: .init())
    
//    lazy var midBgView = UIView()
    /// 日历选择的view
    lazy var monthView = JTACMonthView()
    
    lazy var nowIndicatorColor: UIColor = XWHCalendarHelper.curColor
    lazy var nowIndicatorBgColor: UIColor = btnBgColor.withAlphaComponent(0.12)
    
    var isShowSelectedIndicatorOnNow = false
    
    /// 日历开始的时间
    private var _startDate = XWHCalendarHelper.startDate
    var startDate: Date {
        get {
            _startDate
        }
        set {
            _startDate = newValue
            preNextView.minBeginDate = newValue
            monthView.reloadData()
        }
    }
    /// 日历的结束时间
    private var _stopDate = Date()
    var stopDate: Date {
        get {
            _stopDate
        }
        set {
            _stopDate = newValue
            preNextView.maxBeginDate = newValue
            monthView.reloadData()
        }
    }
    
    /// 选择的日期
    lazy var sDate = Date() {
        didSet {
            curBeginDate = sDate.monthBegin
        }
    }
    /// 选择日期 日的开始时间
    var sBeginDate: Date {
        sDate.dayBegin
    }
    
    /// 当前的年月开始日期
    lazy var curBeginDate = Date().monthBegin
    
    var selectHandler: XWHCalendarSelectDateHandler?
    var scrollDateHandler: XWHCalendarSelectDateHandler?

    lazy var existDataDateItems: [XWHHealthyExistDataDateModel] = [] {
        didSet {
            monthView.reloadData(withAnchor: curBeginDate, completionHandler: nil)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubViews()
        relayoutSubViews()
        
        configEventAction()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    func `set`(sStartDate: Date?, sStopDate: Date?) {
        if sStartDate == nil && sStopDate == nil {
            return
        }
        
        if let tStartDate = sStartDate {
            _startDate = tStartDate
            preNextView.minBeginDate = tStartDate
        }
        if let tStopDate = sStopDate {
            _stopDate = tStopDate
            preNextView.maxBeginDate = tStopDate
        }
        
        monthView.reloadData()
    }
    
    @objc func addSubViews() {
        addSubview(topBgView)
        addSubview(preNextView)
        addSubview(weekIndicatiorView)
        
        topBgView.backgroundColor = bgColor
        monthView.backgroundColor = bgColor
        addSubview(monthView)
        
        configMonthView()
        
        registerViews()
    }
    
    
    // MARK: - 日历配置
    @objc func configMonthView() {
        monthView.calendarDataSource = self
        monthView.calendarDelegate = self
        
        monthView.minimumLineSpacing = 1
        monthView.minimumInteritemSpacing = 1
        monthView.showsVerticalScrollIndicator = false
        monthView.showsHorizontalScrollIndicator = false
        monthView.cellSize = 0
        monthView.scrollingMode = .stopAtEachSection
        monthView.scrollDirection = .horizontal
        
//        monthView.isScrollEnabled = false
        let now = Date()
        monthView.scrollToDate(now, triggerScrollToDateDelegate: false, animateScroll: false)
        monthView.selectDates([sDate], triggerSelectionDelegate: false)
        preNextView.curBeginDate = now.monthBegin
    }
    
    @objc func registerViews() {
        monthView.register(cellWithClass: XWHCalendarDayCTCell.self)
    }
    
    @objc func relayoutSubViews() {
        preNextView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(25)
        }
        weekIndicatiorView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(32)
            make.top.equalTo(preNextView.snp.bottom).offset(22)
        }
        
        topBgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(weekIndicatiorView)
        }
        
        monthView.snp.makeConstraints { make in
            make.top.equalTo(weekIndicatiorView.snp.bottom).offset(4)
            make.bottom.equalToSuperview().inset(4)
            make.left.right.equalTo(weekIndicatiorView)
        }
    }
    
    // MARK: - JTACMonthViewDataSource & JTACMonthViewDelegate
    func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
        log.debug("日历控件 startDate = \(startDate), stopDate = \(stopDate) \n")
        let parameters = ConfigurationParameters(startDate: startDate, endDate: stopDate, numberOfRows: 6, calendar: Calendar.current, generateInDates: .forAllMonths, generateOutDates: .tillEndOfRow, firstDayOfWeek: XWHCalendarHelper.firstDayOfWeek, hasStrictBoundaries: true)

        return parameters
    }
    
    func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
    }
    
    func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
        let cell = calendar.dequeueReusableCell(withClass: XWHCalendarDayCTCell.self, for: indexPath)
        
        cell.textLb.text = cellState.text
        
        cell.textLb.textColor = XWHCalendarHelper.normalColor
        cell.nowIndicator.isHidden = true
        cell.dotIndicator.isHidden = true
        cell.selectedIndicator.isHidden = true

        let cellBeginDate = cellState.date.dayBegin
        let nowBeginDate = Date().dayBegin
        
        if nowBeginDate >= cellBeginDate { // 过去或当前的月份
            if nowBeginDate == cellBeginDate { // 今天
                cell.nowIndicator.isHidden = false
                cell.nowIndicator.layer.backgroundColor = nowIndicatorBgColor.cgColor
                cell.textLb.textColor = nowIndicatorColor
            } else { // 过去
                if cellState.dateBelongsTo != .thisMonth {
                    cell.textLb.textColor = XWHCalendarHelper.disableColor
                }
            }
            
            for iItem in existDataDateItems {
                for jItem in iItem.items {
                    if jItem.dayBegin == cellBeginDate {
                        cell.dotIndicator.isHidden = false
                    }
                }
            }
        } else { // 未来的日子
            cell.dotIndicator.isHidden = true
            cell.textLb.textColor = XWHCalendarHelper.disableColor
        }
        
        if cellBeginDate == sBeginDate {
            if cellBeginDate != nowBeginDate {
                cell.selectedIndicator.isHidden = false
            } else {
                if isShowSelectedIndicatorOnNow {
                    cell.selectedIndicator.isHidden = false
                }
            }
        }
        
        return cell
    }
    
    func calendar(_ calendar: JTACMonthView, shouldSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) -> Bool {
//        if cellState.dateBelongsTo != .thisMonth {
//            return false
//        }
//
//        if cellState.date.isInFuture {
//            return false
//        }
//
//        if cellState.date.dayBegin == sBeginDate {
//            if cellState.isSelected {
//                return false
//            }
//        }
        
        return true
    }
    
    func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) {
        if cellState.dateBelongsTo != .thisMonth {
            return
        }
        
        if cellState.date.isInFuture {
            return
        }
        
        if cellState.date.dayBegin == sBeginDate {
            return
        }
        
        sDate = cellState.date.dayBegin
        calendar.reloadData(withAnchor: sDate, completionHandler: nil)
        
//        guard let cell = cell as? XWHCalendarDayCTCell else {
//            return
//        }
//
//        if cellState.date.dayBegin == sBeginDate {
//            cell.selectedIndicator.isHidden = false
//        }
        
        selectHandler?(sDate)
    }
    
    func calendar(_ calendar: JTACMonthView, shouldDeselectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) -> Bool {
//        if cellState.dateBelongsTo != .thisMonth {
//            return false
//        }
//
//        if cellState.date.isInFuture {
//            return false
//        }
//
//        if cellState.date.dayBegin == sBeginDate, !cellState.isSelected {
//            return false
//        }
        
        return true
    }
    
    func calendar(_ calendar: JTACMonthView, didDeselectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath) {
//        guard let cell = cell as? XWHCalendarDayCTCell else {
//            return
//        }
//
//        if cellState.date.dayBegin == sBeginDate {
//            cell.selectedIndicator.isHidden = true
//        }
    }

    func calendar(_ calendar: JTACMonthView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
        guard let fDate = visibleDates.monthDates.first?.date else {
            return
        }
        
        self.curBeginDate = fDate.monthBegin
        preNextView.curBeginDate = fDate.monthBegin
        scrollDateHandler?(fDate)
    }
    
//    func sizeOfDecorationView(indexPath: IndexPath) -> CGRect {
//        let stride = monthView.frame.width * CGFloat(indexPath.section)
//        return CGRect(x: stride + 5, y: 5, width: monthView.frame.width - 10, height: monthView.frame.height - 10)
//    }

}

// MARK: - ConfigEventAction
extension XWHCalendarDayView {
    
    private func configEventAction() {
        preNextView.selectHandler = { [unowned self] cbDate, aType in
            self.curBeginDate = cbDate
            if aType == .pre {
                self.monthView.scrollToSegment(.previous)
            } else {
                self.monthView.scrollToSegment(.next)
            }
        }
    }
    
}
